<template>
  <div class="main">
    <!-- 信息公告 -->
    <el-dialog
  :visible.sync="dialogVisible"
  width="30%">
  <el-form :model="ruleForm" ref="ruleForm">
    <el-form-item label="描述" prop="title">
    <el-input v-model="ruleForm.title"></el-input>
  </el-form-item>
  <el-form-item label="公告内容" prop="desc">
    <el-input type="textarea" v-model="ruleForm.desc"></el-input>
  </el-form-item>
  <el-button type="primary" @click="submitForm('ruleForm')"  plain>提交</el-button>
  </el-form>
</el-dialog>
<!-- 新闻资讯 -->
<el-dialog
  :visible.sync="newsDialogVisible"
  width="30%">
  <el-form :model="newsForm" ref="newsForm">
    <el-form-item label="描述" prop="title">
    <el-input v-model="newsForm.title"></el-input>
  </el-form-item>
  <el-form-item label="链接" prop="desc">
    <el-input type="textarea" v-model="newsForm.desc"></el-input>
  </el-form-item>
  <el-button type="primary" @click="newsSubmitForm('newsForm')"  plain>提交</el-button>
  </el-form>
</el-dialog>
<!-- 轮播图上传 -->
<el-dialog
  :visible.sync="carouselDialogVisible"
  width="30%"
  :before-close="overTime">
  <el-input
  type="textarea"
  :rows="2"
  placeholder="请输入链接"
  v-model="picAddr.picLink">
</el-input>
  <el-upload
  ref="upPic"
  class="avatar-uploader"
  :action="evUrl+'zj/addPicInfo'"
  :limit=1
  :data="picAddr"
  :auto-upload="false"
  :on-remove="removeFile"
  :on-change="picPre">
  <img v-if="imageUrl" :src="imageUrl" class="avatar">
  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
<el-button @click="makePic" type="primary">提交</el-button>
</el-dialog>
    <div class="center">
      <div class="info">
        <el-tooltip class="item" effect="dark" content="轮播图" placement="top-start"><div class="one"><el-button @click="one" class="el-icon-picture-outline" style="height:100%;width:100%;font-size:100px" type="primary"></el-button></div></el-tooltip>
        <el-tooltip class="item" effect="dark" content="公告信息" placement="top-start"><div class="two"><el-button @click="two" style="height:100%;width:100%;font-size:100px" class="el-icon-chat-line-square" type="success"></el-button></div></el-tooltip>
        <el-tooltip class="item" effect="dark" content="相关资讯" placement="top-start"><div class="three"><el-button @click="three" style="height:100%;width:100%;font-size:100px" class="el-icon-notebook-1" type="info"></el-button></div></el-tooltip>
      </div>
    </div>
  </div>
</template>
<script>
import { addOpenInfo, addNews } from '../api/info.js'
export default {
  data () {
    return {
      evUrl: this.$evUrl,
      dialogVisible: false,
      newsDialogVisible: false,
      carouselDialogVisible: false,
      imageUrl: '',
      // 图片地址
      picAddr: {
        picLink: ''
      },
      ruleForm: {
        title: '',
        desc: ''
      },
      newsForm: {
        title: '',
        desc: ''
      }
    }
  },
  methods: {
    // 轮播图
    one () {
      this.carouselDialogVisible = true
      // console.log('one')
    },
    // 图片上传前
    picPre (file, fileList) {
      // console.log(window.URL.createObjectURL(file.raw))
      // console.log(file, fileList)
      this.imageUrl = window.URL.createObjectURL(file.raw)
      // console.log(this.imageUrl)
    },
    // 移除图片
    removeFile () {
      // console.log('你好啊')
      this.imageUrl = ''
    },
    // 关闭单框前
    overTime () {
      // console.log('你好')
      this.$refs.upPic.clearFiles()
      this.imageUrl = ''
      this.picAddr.picLink = ''
      this.carouselDialogVisible = false
    },
    // 手动上传
    makePic () {
      // console.log('hello')
      this.$refs.upPic.submit()
      this.overTime()
    },
    // 信息公告
    two () {
      this.dialogVisible = true
      // console.log('two')
    },
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addOpenInfo(this.ruleForm).then(({ data }) => {
            if (data.status === 200) {
              this.$message.success(data.message)
              this.$refs[formName].resetFields()
              this.dialogVisible = false
            }
          })
        } else {
          // console.log('error submit!!')
          return false
        }
      })
    },
    // 新闻资讯
    three () {
      this.newsDialogVisible = true
      // console.log('three')
    },
    newsSubmitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addNews(this.newsForm).then(({ data }) => {
            if (data.status === 200) {
              this.$message.success(data.message)
              this.$refs[formName].resetFields()
              this.newsDialogVisible = false
            }
          })
        } else {
          // console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style scoped lang='less'>
.main{
  .center{
    background: blue;
    position: absolute;
    left: 50%;
    top: 50%;
    transform:translate(-50%,-50%);
    height: 150px;
    width: 500px;
    .info{
      display: flex;
      justify-content: space-between;
      .one{
        background: rebeccapurple;
      }
      .two{
        background: red;
      }
      .three{
        background: gray;
      }
    }

  }
  .one,.two,.three{
      margin: 5px;
      height: 140px;
      width: 140px;
    }
    .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
}

</style>
